package com.example.carrierapp.ui.person.modifypass;

import android.app.Activity;
import android.content.Context;

import com.example.carrierapp.base.BasePresenter;
import com.example.carrierapp.bean.home.SearchCarBean;
import com.example.carrierapp.bean.person.ImageCodeBean;
import com.example.carrierapp.bean.person.ModifyPassRequestBody;
import com.example.carrierapp.bean.person.PassNextStepRequestBody;
import com.example.carrierapp.bean.person.PhoneCodeRequestBody;
import com.example.carrierapp.net.ApiMiddleHelper;
import com.example.carrierapp.net.listener.DataCallBackListener;
import com.example.carrierapp.util.LogUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Flowable;

/**
 * Created by Administrator on 2017/1/4.
 */
public class ModifyPassPresenter extends BasePresenter<ModifyPassContract.View> implements ModifyPassContract.Presenter{
    private SearchCarBean searchCarBean;
    private Context mContext;
    public ModifyPassPresenter(Activity context) {
        mContext=context;
    }

    @Override
    public void start() {

    }
    @Override
    public void getImgaeCode() {
        Flowable<JsonObject> searchFilter = apiStrores.getImgaeCode();

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext,searchFilter, new DataCallBackListener() {
            @Override
            public void onSuccess(String json) {
                LogUtil.d("getImgaeCode-onSuccess-" + json);
                try {
                    ImageCodeBean dispatchToatBean = new Gson().fromJson(json, new TypeToken<ImageCodeBean>() {
                    }.getType());
                    if (getView() != null) {
                        getView().getImgaeCodeSuccess(dispatchToatBean);

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (getView() != null) {
                        getView().getImgaeCodeField("数据获取失败");
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("getImgaeCode-onFailure-" + msg);
                if (getView() != null) {
                    getView().getImgaeCodeField( msg);
                }
            }
        });
    }

    @Override
    public void getMessageCode(String phone, String imageCode, String guide) {
        PhoneCodeRequestBody body=new PhoneCodeRequestBody();
        body.phone=phone;
        body.imageCode=imageCode;
        body.guid=guide;
        body.isChangePhone=false;

        Flowable<JsonObject> searchFilter = apiStrores.getMessageCode(body);
        ApiMiddleHelper.getInstance().toNetIo2Main(mContext,searchFilter, new DataCallBackListener() {
            @Override
            public void onSuccess(String json) {
                LogUtil.d("getMessageCode-onSuccess-" + json);
                try {
                    if (getView() != null) {
                        getView().getMessageCodeSuccess();

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (getView() != null) {
                        getView().getMessageCodeField("数据获取失败");
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("getMessageCode-onFailure-" + msg);
                if (getView() != null) {
                    getView().getMessageCodeField( msg);
                }
            }
        });
    }

    @Override
    public void changePass(String phone, String newPassword, String repeatPassword,String code) {

        ModifyPassRequestBody body=new ModifyPassRequestBody();
        body.phone=phone;
        body.newPassword=newPassword;
        body.phoneCode=code;
        body.repeatPassword=repeatPassword;

        Flowable<JsonObject> searchFilter = apiStrores.changePass(body);

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext,searchFilter, new DataCallBackListener() {
            @Override
            public void onSuccess(String json) {
                LogUtil.d("changePass-onSuccess-" + json);
                try {
                    if (getView() != null) {
                        getView().changePassSuccess();

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (getView() != null) {
                        getView().changePassField("数据获取失败");
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("changePass-onFailure-" + msg);
                if (getView() != null) {
                    getView().changePassField( msg);
                }
            }
        });
    }

    @Override
    public void nextStep(String phone, String imageCode, String guide, String phoneCode) {
        PassNextStepRequestBody body=new PassNextStepRequestBody();
        body.phone=phone;
        body.guid=guide;
        body.phoneCode=phoneCode;
        body.imageCode=imageCode;

        Flowable<JsonObject> searchFilter = apiStrores.passNextStep(body);

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext,searchFilter, new DataCallBackListener() {
            @Override
            public void onSuccess(String json) {
                LogUtil.d("changePass-onSuccess-" + json);
                try {
                    if (getView() != null) {
                        getView().nextStepSuccess();

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (getView() != null) {
                        getView().nextStepField("数据获取失败");
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("changePass-onFailure-" + msg);
                if (getView() != null) {
                    getView().nextStepField( msg);
                }
            }
        });
    }
}
